package logic

import (
	"context"
	"fmt"
	"we-chat-login-sdk/models/cache"
	"we-chat-login-sdk/models/mysql"

	"we-chat-login-sdk/watchrpc/internal/svc"
	"we-chat-login-sdk/watchrpc/watchrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *watchrpc.LoginRequest) (*watchrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var fuser mysql.User
	if fuser.ID == 0 {
		return nil, fmt.Errorf("此账号未注册,抢先前往注册")
	}
	result, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile).Result()
	if result != in.SendSmsCode {
		return nil, fmt.Errorf("短信验证码错误")
	}
	if fuser.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}
	return &watchrpc.LoginResponse{
		UserId: int64(fuser.ID),
	}, nil
}
